/*
 * Transaction.cpp
 *
 *  Created on: May 3, 2010
 *      Author: Sequoyha
 */

#include "Transaction.h"

//CONSTRUCTORS---------------------------------------------------------
Transaction::Transaction(){}
Transaction::Transaction(Winery store):head(NULL),currentWinery(store),
				grandTotal(0.00){}

/**********************************************************************
 * addSale
 *
 * Adds an item to the total sale from a store and calculates total
 * sales amount. Creates a linked list of all items bought from one
 * store.
 *
 * Precondition: Winery and stock list must be valid when passed to
 * constructor. Item number and total purchased must be passed to
 * function.
 *
 * Post-Condition: Linked list with transaction amounts is created/
 *********************************************************************/
void Transaction::addSale(int selection, int qty)
{
	Purchase *thisPurchase;		//Pointer for linked list
	thisPurchase = new Purchase;

	wines tWines;		//Wines vector for making sale
	wines::iterator w;	//Wines iterator
	tWines = currentWinery.getStock();	//loading wines vector
	//setting iterator to vector beginning
	w = tWines.begin();
	//loop to get to appropriate item for sales
	for(int i = 0; i < selection-1; i++)
	{
		w++;
	}
	//Assign purchase information
	thisPurchase->item = w->getWineName();
	thisPurchase->quantity = qty;
	thisPurchase->price = w->getPrice();
	thisPurchase->total = w->getPrice() * qty;

	//Increment grand total for later reporting
	grandTotal +=thisPurchase->total;
	//Linking list
	thisPurchase->next = head;
	head = thisPurchase;
}

/******************************************************************
 * PrintSales
 *
 * Prints sales information to console and to a text file named for
 * the winery as well so the user can take a hard copy receipt.
 *
 * Preconditions: Linked list must be populated
 *
 * Post-Conditions: Text file is created and information is printed
 * to the screen
 ******************************************************************/
void Transaction::PrintSales()
{
	Purchase *receipt;		//Pointer for traversing list
	ofstream outFile;		//output file
	char fName[40];			//c str for making file name(s)
	//Initializing variables
	receipt = head;
	string lrt = currentWinery.getWineryName();
	//Concatenate string to create output file true name
	strcpy(fName, lrt.c_str());
	strcat(fName,".txt");

	//Open output file
	outFile.open(fName);

	//OUTPUT-----------------------------------------------------------
	//HEADER SECTION
	cout << currentWinery.getWineryName() << endl;
	cout << setw(30) << left << "ITEM" << setw(8)<< "Price"
		<< setw(8) << "Qty." << setw(8) << "Total" << endl;
	outFile << currentWinery.getWineryName() << endl;
	outFile << setw(30) << left << "ITEM" << setw(8) << "Price"
			<< setw(8) << "Qty." << setw(8) << "Total" << endl;

	//Sales information section
	while(receipt !=NULL)
	{
		//Console--------------------------------------
		cout << setw(30) << left << receipt->item << " $";
		cout << setw(8) << fixed << setprecision(2) << receipt->price;
		cout << setw(5) << receipt->quantity << " $";
		cout << setw(5) << receipt->total << endl;

		//File-----------------------------------------
		outFile << setw(30) << left << receipt->item << " $";
		outFile << setw(8) << fixed << setprecision(2) << receipt->price;
		outFile << setw(5) << receipt->quantity << " $";
		outFile << setw(5) << receipt->total << endl;

		//Move pointer to access next record
		receipt = receipt->next;
	}
	//Pointer management
	receipt = NULL;
	delete receipt;
	//OUTPUT FOOTER-----------------------------------------
	//Console-----------------------------------------------
	cout << setw(43) << right << "GRAND TOTAL:";
	cout << "$" << setw(8)<< fixed << setprecision(2)
		<< grandTotal << endl << endl;
	//File-------------------------------------------------
	outFile << setw(43) << right << "GRAND TOTAL:";
	outFile << "$" << setw(8)<< fixed << setprecision(2)
			<< grandTotal << endl << endl;
	outFile << "ALL SALES FINAL  - IF YOU REALLY THOUGHT THAT YOU ";
	outFile << "COULD RETURN\nA FAKE BOTTLE OF WINE THEN YOU ARE AN IDIOT";
		//Close output file
	outFile.close();
}

//Returns grand total to calling function for tallying total
//Across all sales/stores
float Transaction::getGrandTotal(){return grandTotal;}
